package com.aishua.fire.controller;

import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.anno.PermMapping;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.addressBook.AddressBookInfo;
import com.aishua.fire.entity.emergencyPre.EmergencyPreDetailsRespInfo;
import com.aishua.fire.entity.organization.AddOrganizationInfo;
import com.aishua.fire.entity.organization.AddRoleInfo;
import com.aishua.fire.entity.organization.DeleteUserToOrganizationInfo;
import com.aishua.fire.entity.organization.OrganizationInfo;
import com.aishua.fire.entity.organization.UserToOrganizationInfo;
import com.aishua.fire.entity.selectrole.SelectAddressBean;
import com.aishua.fire.entity.selectrole.SelectRoleBean;
import com.aishua.fire.service.OrganizationService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "组织结构管理",tags = {"组织结构管理模块接口"})
@RestController
@Slf4j
public class OrganizationManagerController
{

	@Autowired
	private OrganizationService organService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_新增组织结构部门信息")
	@PostMapping(value = {"/organization"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody @Valid AddOrganizationInfo info, BindingResult result) throws ValidateException
	{
		log.info("组织结构管理_新增部门信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = organService.save(info);
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_根据类型查询组织结构树")
	@GetMapping(value = {"/organization"} )
	public ResponseEntity<JsonArrayResult> select(@RequestParam(name = "type") String type)
	{
		log.info("组织结构管理_查询组织结构树 :{}",type);
		List<OrganizationInfo> list = organService.select(type);
		
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_查询当前组织结构下所有员工")
	@GetMapping(value = {"/organization/user/list"} )
	public ResponseEntity<JsonArrayResult> selectOrgList(@RequestParam(name = "id") String id, @RequestParam(name = "type") String type)
	{
		log.info("组织结构管理_查询当前组织结构下所有员工:{}",id);
		
		List<AddressBookInfo> list = organService.selectAddressBookInfoList(id, type);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@PermMapping(onlyLogin = true)
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_修改组织结构部门信息")
	@PutMapping(value = {"/organization"} )
	public ResponseEntity<JsonObjectResult> update(@RequestBody @Valid AddOrganizationInfo info, BindingResult result) throws ValidateException
	{
		log.info("组织结构管理_修改组织结构部门信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		organService.update(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_删除组织结构树部门信息")
	@DeleteMapping(value = {"/organization"} )
	public ResponseEntity<JsonObjectResult> delete(@RequestBody AddOrganizationInfo info) 
	{
		log.info("组织结构管理_删除部门信息:{}",info.getId());
		organService.delete(info.getId());
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_查询不属于当前节点的通讯录列表")
	@GetMapping(value = {"/organization/userList"} )
	public ResponseEntity<JsonObjectResult> selectUserIsNotInOrganization(@RequestParam(name = "id") String id) throws ValidateException
	{
		log.info("组织结构管理_查询不属于当前节点的通讯录列表:{}",id);
		List<AddressBookInfo> list = organService.selectUserIsNotInOrganization(id);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(list);
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_添加当前通讯中某位员工")
	@PostMapping(value = {"/organization/user"} )
	public ResponseEntity<JsonObjectResult> addUserToOrganization(@RequestBody UserToOrganizationInfo info, BindingResult result) throws ValidateException
	{
		log.info("检查项目分类目录_添加当前通讯中某位员工:{}",info);
		organService.saveUserToOrganization(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }

	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="组织结构管理_移除当前组织结构下某位员工")
	@DeleteMapping(value = {"/organization/user"} )
	public ResponseEntity<JsonObjectResult> deleteUserToOrganization(@RequestBody DeleteUserToOrganizationInfo info, BindingResult result) throws ValidateException
	{
		log.info("组织结构管理_移除当前组织结构下某位员工:{}",info);
		organService.deleteUserToOrganization(info);
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }
	
	
	
	
	@SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_新增外部组织结构部门信息")
    @PostMapping(value = {"/organization/other"} )
    public ResponseEntity<JsonObjectResult> addother(@RequestBody @Valid AddOrganizationInfo info, BindingResult result) throws ValidateException
    {
        log.info("组织结构管理_新增外部部门信息:{}",info);
        validateUtil.respErrorMsg(result);//非空验证
        boolean flag = organService.save(info);
        JsonObjectResult r = null;
        if(flag)
        {
            r = JsonObjectResult.makeSuccessResult(null);
        }
        else
        {
            r = JsonObjectResult.makeErrorResult("名称重复");
        }
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @PermMapping(onlyLogin = true)
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_根据外部类型查询组织结构树")
    @GetMapping(value = {"/organization/other"} )
    public ResponseEntity<JsonArrayResult> selectother(@RequestParam(name = "type") String type)
    {
        log.info("组织结构管理_查询外部组织结构树 :{}",type);
        List<OrganizationInfo> list = organService.select(type);
        
        JsonArrayResult r = JsonArrayResult.makeSuccessResult(list, 0);

        return ResponseEntity.ok(r);
    }
    
    
    @SuppressWarnings("rawtypes")
    @PermMapping(onlyLogin = true)
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_修改外部组织结构部门信息")
    @PutMapping(value = {"/organization/other"} )
    public ResponseEntity<JsonObjectResult> updateother(@RequestBody @Valid AddOrganizationInfo info, BindingResult result) throws ValidateException
    {
        log.info("组织结构管理_修改外部组织结构部门信息:{}",info);
        validateUtil.respErrorMsg(result);//非空验证
        organService.update(info);
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);

        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_删除外部组织结构树部门信息")
    @DeleteMapping(value = {"/organization/other"} )
    public ResponseEntity<JsonObjectResult> deleteother(@RequestBody AddOrganizationInfo info) 
    {
        log.info("组织结构管理_删除外部部门信息:{}",info.getId());
        organService.delete(info.getId());
        
        JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_部门管理_新增角色信息")
    @PostMapping(value = {"/role"} )
    public ResponseEntity<JsonObjectResult> addRole(@RequestBody @Valid AddRoleInfo info, BindingResult result) throws ValidateException
    {
        log.info("组织结构管理_部门管理_新增角色信息:{}",info);
        validateUtil.respErrorMsg(result);//非空验证
        boolean flag = organService.addRole(info);
        JsonObjectResult r = null;
        if(flag)
        {
            r = JsonObjectResult.makeSuccessResult(null);
        }
        else
        {
            r = JsonObjectResult.makeErrorResult("名称重复");
        }
        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @PermMapping(onlyLogin = true)
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="组织结构管理_部门管理_根据部门节点查询角色列表信息")
    @GetMapping(value = {"/role"} )
    public ResponseEntity<JsonArrayResult<AddRoleInfo>> selectRole(@RequestParam(name = "o_id") String o_id)
    {
        log.info("组织结构管理_部门管理_查询角色信息 :{}",o_id);
        List<AddRoleInfo> list = organService.selectRole(o_id);
        
        JsonArrayResult<AddRoleInfo> r=JsonArrayResult.makeSuccessResult(list, 0);

        return ResponseEntity.ok(r);
    }
    
    @SuppressWarnings("rawtypes")
    @PermMapping(onlyLogin = true)
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="部门组织角色下拉框")
    @GetMapping(value = {"/roleSelect"} )
    public ResponseEntity<JsonArrayResult<SelectRoleBean>> roleSelect(@RequestParam(name = "o_id") String o_id)
    {
        log.info("部门组织角色下拉框 :{}", o_id);
        List<SelectRoleBean> list = organService.roleSelect(o_id);
        JsonArrayResult<SelectRoleBean> r=JsonArrayResult.makeSuccessResult(list, 0);

        return ResponseEntity.ok(r);
    }
    
    
    
    @SuppressWarnings("rawtypes")
    @PermMapping(onlyLogin = true)
    @ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
    @ApiOperation(value="职责组织通讯录树形结构")
    @GetMapping(value = {"/addressBookSelect"} )
    public ResponseEntity<JsonArrayResult<SelectRoleBean>> addressBookSelect(@RequestParam(name = "o_id") String o_id)
    {
        log.info("职责组织通讯录树形结构 :{}", o_id);
        List<SelectRoleBean> list = organService.addressBookSelect(o_id);
        JsonArrayResult<SelectRoleBean> r=JsonArrayResult.makeSuccessResult(list, 0);

        return ResponseEntity.ok(r);
    }
}
